package com.ruoyi.web.controller.gasoline;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ChildGasoline;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IChildGasolineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-06-23
 */
@Controller
@RequestMapping("/ruoyi/gasoline")
public class ChildGasolinePlController extends BaseController
{
    private String prefix = "ruoyi/Childgasoline";

    @Autowired
    private IChildGasolineService childGasolineService;

    @RequiresPermissions("ruoyi:gasoline:view")
    @GetMapping()
    public String gasoline()
    {
        return prefix + "/ChildgasolinePL";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:gasoline:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChildGasoline childGasoline)
    {
        startPage();
        childGasoline.setPeople(ShiroUtils.getLoginName());
        List<ChildGasoline> list = childGasolineService.selectChildGasolineList(childGasoline);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:gasoline:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChildGasoline childGasoline)
    {
        List<ChildGasoline> list = childGasolineService.selectChildGasolineList(childGasoline);
        ExcelUtil<ChildGasoline> util = new ExcelUtil<ChildGasoline>(ChildGasoline.class);
        return util.exportExcel(list, "Childgasoline");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/addpl";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:gasoline:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, ChildGasoline childGasoline)throws IOException
    {
        //上传文件路径
        String filePach = Global.getUploadPath();
        //上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePach, file);
        childGasoline.setChFile(fileName);
        childGasoline.setcStatus(0L);
        childGasoline.setDepa(ShiroUtils.getdept());
        childGasoline.setPicture(ShiroUtils.getavatar());
        childGasoline.setPeople(ShiroUtils.getLoginName());
        return toAjax(childGasolineService.insertChildGasoline(childGasoline));
    }

    /**
     * 提交申请
     */
    @RequiresPermissions("ruoyi:gasoline:submitApply")
    @Log(title = "报销业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id, ChildGasoline childGasoline) {

        childGasoline.setChId(id);
        childGasoline.setcStatus(1L);
        childGasoline.setText(" ");
        int succ=childGasolineService.updateChildGasoline(childGasoline);
        if(succ==1){
            return success("提交成功");
        }else{
            return error("提交失败");
        }
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{chId}")
    public String edit(@PathVariable("chId") Long chId, ModelMap mmap)
    {
        ChildGasoline childGasoline = childGasolineService.selectChildGasolineById(chId);
        mmap.put("childGasoline", childGasoline);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:gasoline:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChildGasoline childGasoline)
    {
        return toAjax(childGasolineService.updateChildGasoline(childGasoline));
    }

    /**
     * 撤销
     * @param id
     * @return
     */
    @RequiresPermissions("ruoyi:gasoline:removes")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/removes")
    @ResponseBody
    public AjaxResult removes(Long id)
    {
        int succ= childGasolineService.deleteChildGasolineById(id);
        if(succ==1){
            return success("撤销成功");
        }else{
            return error("撤销失败");
        }

    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:gasoline:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(childGasolineService.deleteChildGasolineByIds(ids));
    }
}
